package com.rybbaby.tss.core.verifier;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

/**
 * @description 时间验证器(默认格式：YYYY-MM-dd HH:mm:ss)
 * 
 * @author 谢进伟
 * 
 * @createDate 2016年3月31日 上午9:37:11
 */
public class DateVerifier extends AbstractVerifier {
	
	/**
	 * 时间格式
	 * 
	 * @return
	 */
	private String format = "YYYY-MM-dd HH:mm:ss";
	
	/**
	 * 是否允许为空值
	 * 
	 * @return
	 */
	private boolean nullable = true;
	
	/**
	 * 获取 是否允许为空值
	 * 
	 * @return 是否允许为空值
	 */
	public boolean isNullable() {
		return this.nullable;
	}
	
	/**
	 * 设置 是否允许为空值
	 * 
	 * @param nullable
	 *            是否允许为空值
	 */
	public void setNullable(boolean nullable) {
		this.nullable = nullable;
	}
	
	@Override
	public boolean validate(Object params) {
		return validate(params , nullable , null , new ValidationRule() {
			
			@Override
			public boolean check(Object params) {
				boolean result = checkDate(params);
				if(!result) {
					DateVerifier.this.errorMsg = "该值格式必须为：" + format;
				}
				return result;
			}
		});
	}
	
	private boolean checkDate(Object params) {
		String value = params.toString();
		String formats [] = format.split(" ");
		String sections [] = value.split(" ");
		String dateFormat1 = formats[0];
		String timeFormat1 = (formats.length > 1) ? formats[1] : null;
		String amOrPm = (formats.length > 2) ? formats[2] : null;
		String date1 = sections[0];
		String time1 = (sections.length > 1) ? sections[1] : null;
		
		if(formats.length != sections.length) {
			return false;
		}
		// 确定分隔符
		String separator = (date1.indexOf("/") != -1) ? "/" : ((date1.indexOf('-') != -1) ? "-" : null);
		if(separator == null) {
			return false;
		}
		// 确定日期
		String [] date = date1.split(separator);
		String [] dateFormat = dateFormat1.split(separator);
		int year_index = ArrayUtils.indexOf(dateFormat , "YYYY");
		int month_index = ArrayUtils.indexOf(dateFormat , "MM");
		int day_index = ArrayUtils.indexOf(dateFormat , "dd");
		String year = date[year_index];
		String month = date[month_index];
		String day = date[day_index];
		// 确定时间
		String minutes = null , hours = null , seconds = null;
		if(StringUtils.isNotBlank(timeFormat1)) {
			String [] timeFormat = timeFormat1.split(":") , time = time1.split(":");
			if(timeFormat.length != time.length) {
				return false;
			}
			hours = time.length > 0 ? time[0] : null;
			minutes = time.length > 1 ? time[1] : null;
			seconds = time.length > 2 ? time[2] : null;
			// 验证 seconds
			if(StringUtils.isNotBlank(seconds)) {
				int seconds_int = Integer.valueOf(seconds);
				if(seconds_int < 0 || seconds_int > 60) {
					return false;
				}
			}
			// 验证 hours
			if(StringUtils.isNotBlank(hours)) {
				int hours_int = Integer.valueOf(hours);
				if(hours_int < 0 || hours_int >= 24 || (StringUtils.isNotBlank(amOrPm) && hours_int > 12)) {
					return false;
				}
			}
			// 验证 minutes
			if(StringUtils.isNotBlank(minutes)) {
				int minutes_int = Integer.valueOf(minutes);
				if(minutes_int < 0 || minutes_int > 59) {
					return false;
				}
			}
		}
		// 验证 day, month, and year
		int day_int = Integer.valueOf(day);
		int month_int = Integer.valueOf(month);
		int year_int = Integer.valueOf(year);
		
		if(year_int < 1000 || year_int > 9999 || month_int == 0 || month_int > 12) {
			return false;
		}
		int [] numDays = {31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31};
		// 更新的天数在闰年的2月
		if(year_int % 400 == 0 || (year_int % 100 != 0 && year_int % 4 == 0)) {
			numDays[1] = 29;
		}
		// 验证day
		return(day_int > 0 && day_int <= numDays[month_int - 1]);
	}
}
